package com.codeyourlife.controller;

import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.codeyourlife.entity.EmailVerify;
import com.codeyourlife.entity.User;
import com.codeyourlife.service.EmailVerifyService;
import com.codeyourlife.service.UserService;

@Controller
public class UserControler {

	@Autowired
	private UserService userService;
	@Autowired
	private EmailVerifyService emailVerifyService;

	@RequestMapping(value = "/register")
	@ResponseBody
	public String Regist(HttpServletRequest request) {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String email = request.getParameter("email");
		String email_captcha = request.getParameter("email_captcha");
		int status = 0;

		// check email_captcha;
		EmailVerify emailVerify = new EmailVerify();
		try {
			status = -1;
			// 在EmailVerify中查找email，以便于获取真实验证码code
			emailVerify = emailVerifyService.findEmailVerifyByEmail(email, "1");
			// 验证码相同
			if (emailVerify.getCode().equals(email_captcha)) {
				// 删除EmailVerify中的此email用户
				emailVerifyService.delEmailVerifyCode(emailVerify.getId());
				// save user information into database;
				Timestamp date_joined = new Timestamp(System.currentTimeMillis());
				;
				String image = "/img/people.jpg";
				User user = new User();
				user.setUsername(username);
				user.setPassword(password);
				user.setEmail(email);
				user.setDate_joined(date_joined);
				user.setImage(image);

				System.out.println(user.toString());
				userService.addUser(user);
				status = 1;
			}
		} catch (Exception e) {
			System.out.println("UserController: " + e.toString());
		}
		return "{\"status\":" + status + "}";
	}

	// login operation
	@RequestMapping(value = "/login")
	@ResponseBody
	public User Login(HttpServletRequest request, HttpSession session) {
		String password = request.getParameter("password");
		String email = request.getParameter("email");
		System.out.println(password + " " + email);

		User userexist = userService.findByName(email);
		System.out.println(userexist);
		if (userexist != null) {
			userexist = userService.logincheck(email, password);
			if (userexist == null) {
				User user = new User();
				user.setStatus("0");
				//user.setUsername("0");
				return user;
				
			} else {
				session.setAttribute("role", userexist);
				userexist.setStatus("1");
				//userexist.setUsername("1");
				return userexist;
			}
		} else {
			User user = new User();
			user.setStatus("-1");
			//user.setUsername("-1");
			return user;
		}
	}

	
	// logout
	@RequestMapping(value = "/logout")
	@ResponseBody
	public String Logout() {
		return null;
	}

	
	// Pwd_reset
	@RequestMapping(value = "/Pwd_reset")
	@ResponseBody
	public String Pwd_reset(HttpServletRequest request) {
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		System.out.println(email + " UserControler:Pwd_reset " + password);
		
		int status=-1;
		try{
		User user=new User();
		user.setEmail(email);
		user.setPassword(password);
		userService.updateUser(user);
		status=1;
		}catch(Exception e){
			System.out.println(" UserControler:Pwd_reset " + e.toString());
		}
		return "{\"status\":"+status+"}";
	}

}
